home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Telnet 2.6.1d1 4⁄26⁄94 Folder / source / vr / vdevice.c next >
Text File  |  1994-02-20  |  6KB  |  211 lines

  1. #ifndef __ALLNU__
  2. #define __ALLNU__
  3.  
  4. #ifdef MPW
  5. #pragma segment ICR
  6. #endif
  7.  
  8. #include "TelnetHeader.h"
  9.  
  10. #include <Palettes.h>
  11. #endif
  12.  
  13. #include "vdevice.h"
  14.  
  15. extern TelInfoRec *TelInfo;
  16. extern SysEnvRec theWorld;                        /* System Environment record */
  17.  
  18. /*************************************************************************/
  19. /* Virtual drawing device code.
  20. *
  21. *  
  22. *  InitVDevice()        fill in the fields of a vdevice and allocate the gdevice.
  23. *  SetVDevice()         set the device and port for off-screen drawing.
  24. *                       this only pushes the old values 1-deep, no stack.
  25. *  UnsetVDevice()       set the device and port back to what they were.
  26. *  TrashVDevice()       dispose and close all fields in the vdevice.
  27. *  ColorVDevice()       set the color palette for the 8-bit vdevice.
  28. */
  29. GDHandle v_savegd;        /* the saved gdevice */
  30. CGrafPtr v_saveport;    /* the saved port from SetPort */
  31.  
  32. /*************************************************************************/
  33. /* InitVDevice
  34. *  allocate an off-screen pixmap and off-screen gdevice which can be drawn
  35. *  in without the palette manager affecting the colors.  If we use MakeITable
  36. *  ourselves and don't install the gdevice into the gdevice list or make it
  37. *  active, then the palette manager ignores it, but QuickDraw works on it.
  38. *
  39. *  set vdev->bounds to the size you need before calling.
  40. *  allocate vdev->bp enough space to hold one byte per pixel of a rectangle that
  41. *  size.
  42. *
  43. *  returns 0 if ok,
  44. *  returns -1 or other negative on other errors.
  45. */
  46. int InitVDevice
  47.   (
  48.     VDevicePtr vdev
  49.   )
  50. {
  51.     GDHandle thegd;
  52.     PixMapHandle pm;
  53.     Rect tr;
  54.     CTabHandle ct;
  55.     int width;
  56.  
  57.     GetPort((GrafPtr *) &v_saveport);
  58.     v_savegd = GetGDevice();            /* get old values */
  59.     
  60.     tr = vdev->bounds;                    /* get size of device to create */
  61.     if (tr.right - tr.left < 1 ||
  62.         tr.bottom - tr.top < 1)
  63.         return(-1);                        /* check for simple mistake */
  64.         
  65.     width = tr.right - tr.left;
  66.     
  67.     if (!vdev->bp)
  68.         return(-2);                        /* another simple mistake */
  69. /*
  70. *  Allocate the off-screen PixMap for drawing a duplicate copy.  The off-screen
  71. *  gdevice, CPort and PixMap form a virtual drawing space with its own color map.
  72. *  For this program, we have chosen to make this virtual space an 8-bit drawing
  73. *  device.  When drawing, use SetVDevice and UnsetVDevice to turn on and off.
  74. */
  75.     thegd = vdev->vgd = NewGDevice( 0, -1 );
  76.     
  77.     pm = (PixMapHandle) NewHandle(sizeof(PixMap));
  78.  
  79.     if (width & 1)    {                    /* must be even */
  80.         --tr.right;
  81.         --width;
  82.     }
  83.     (*pm)->baseAddr = (Ptr) vdev->bp;    /* BYU LSC - get memory to use */
  84.     (*pm)->rowBytes = width | 0x8000;    /* setting high flag bit */
  85.     (*pm)->bounds = tr;
  86.     (*pm)->pixelSize = 8;                /* source is 8-bits */
  87.     (*pm)->pixelType = 0;                /* chunky */
  88.     (*pm)->cmpCount = 1;                /* chunky */
  89.     (*pm)->cmpSize = 8;                    /* chunky */
  90.     (*pm)->planeBytes = 0;                /* chunky */
  91.     (*pm)->pmVersion = 0;                /* chunky */
  92.     (*pm)->packSize = 0;                /* chunky */
  93.     (*pm)->packType = 0;                /* chunky */
  94.     ct = (CTabHandle)NewHandle(sizeof(ColorTable));
  95.     (*pm)->pmTable = ct;
  96.     (*ct)->ctSeed = GetCTSeed();
  97.     (*ct)->ctFlags = 0x8000;
  98.     (*ct)->ctSize = 1;                    /* 1-length color table (empty) */
  99.     
  100.     (*thegd)->gdResPref = 3;            /* inverse table size preferred */
  101.     (*thegd)->gdRect = tr;                /* device size boundary */
  102.  
  103.     (*thegd)->gdPMap = pm;                /* copy pixmap handle */
  104.     
  105.     SetDeviceAttribute(thegd,noDriver,true);
  106.     SetDeviceAttribute(thegd,gdDevType,true);
  107.     
  108.     SetGDevice(thegd);
  109.                         /* CPort inherits from the gdevice, including pixmap */
  110.     if (theWorld.hasColorQD)                        /* BYU */
  111.         OpenCPort(&vdev->vport);        /* BYU - initialize the port struct */
  112.     SetPort((GrafPtr) &vdev->vport);
  113.     ClipRect(&tr);                        /* set clip region */
  114.  
  115. /*
  116. *  Erase the image region in the virtual device. 
  117. */
  118.     EraseRect(&tr);
  119.  
  120. /*
  121. *  Restore the environment.
  122. */
  123.     SetGDevice(v_savegd);
  124.     SetPort((GrafPtr) v_saveport);
  125.     
  126.     return(0);
  127. }
  128.  
  129. /*******************************************************************************/
  130. /* SetVDevice
  131. *  Set the gdevice and port to our off-screen space.
  132. *  Save the old values for unset.
  133. */
  134. SetVDevice(VDevicePtr vdev)
  135. {
  136.  
  137.     GetPort((GrafPtr *) &v_saveport);
  138.     v_savegd = GetGDevice();
  139.     if (!vdev->vgd)
  140.         return(-1);
  141.     SetGDevice(vdev->vgd);
  142.     SetPort((GrafPtr) &vdev->vport);
  143.     
  144.     return(0);
  145. }
  146.  
  147. /*******************************************************************************/
  148. /* UnsetVDevice
  149. *  Set the vdevice back to the saved values.
  150. */
  151. UnsetVDevice(void)
  152. {
  153.     SetGDevice(v_savegd);
  154.     SetPort((GrafPtr) v_saveport);
  155. }
  156.  
  157. /*******************************************************************************/
  158. /* TrashVDevice
  159. *  Get rid of the devices that we created with InitVDevice.
  160. *
  161. *  Remember to free up the vdev->bp after the gdevice is gone.
  162. */
  163. void TrashVDevice
  164.   (
  165.     VDevicePtr vdev
  166.   )
  167. {
  168.     
  169.     (*((*(vdev->vgd))->gdPMap))->baseAddr = NULL;    /* drop old value, bp has a copy */
  170.         
  171.     if (theWorld.hasColorQD)                    /* BYU */
  172.         CloseCPort(&vdev->vport);    /* BYU - lose the cport, disposes the pixmap */
  173.     (*(vdev->vgd))->gdPMap = NULL;    /* destroy second copy of the pixmaphandle */
  174.     
  175.     DisposGDevice(vdev->vgd);        /* disposes current gdevice and pixmap */
  176.     vdev->vgd = NULL;
  177.  
  178. }
  179.  
  180. /*******************************************************************************/
  181. /*  ColorVDevice
  182. *   input:  vdev and 
  183. *        palette handle.
  184. *   
  185. *   Use palette2ctab to install the color table into the off-screen gdevice and
  186. *   make the inverse color table for it.  Also install the palette into the current
  187. *   window.
  188. */
  189. void ColorVDevice
  190.   (
  191.     VDevicePtr vdev,
  192.     PaletteHandle pal
  193.   )
  194. {
  195.     CTabHandle ct;
  196.  
  197.     ct = (*(*(vdev->vgd))->gdPMap)->pmTable;        /* handle from vdevice */
  198.     if (!ct)
  199.         return;
  200.     
  201.     Palette2CTab( pal, ct );
  202.  
  203.     (*ct)->ctSeed = GetCTSeed();                    /* give it a unique seed */
  204.     (*ct)->ctFlags = 0x8000;
  205.     
  206.     MakeITable( ct, (*(vdev->vgd))->gdITable, 3 );    /* 3-bit inverse table  */
  207.  
  208. }
  209.  
  210.